// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DifferentialDriveStateTest.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Test.Robotics.Hardware.DAC
{
    using System.Collections.Generic;

    using Microsoft.Robotics.Hardware.DAC;    
    using Microsoft.Robotics.Numerics;
    using Microsoft.VisualStudio.TestTools.UnitTesting;  

    /// <summary>
    /// Test class for DifferentialDriveState
    /// </summary>
    public class DifferentialDriveStateTest
    {
        /// <summary>
        /// Test differential drive state updates by sending random
        /// differential drive state to the updater
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void DifferentialDriveStateInterpolate()
        {
            DifferentialDriveState startDifferentialDrive = new DifferentialDriveState();
            startDifferentialDrive.LeftWheelPositionInMeters = 1;
            startDifferentialDrive.RightWheelPositionInMeters = 10;
            startDifferentialDrive.TimestampInMilliseconds = 2;
            startDifferentialDrive.TimestampInTicks = 2;
            startDifferentialDrive.DeviceId = 100;

            DifferentialDriveState endDifferentialDrive = new DifferentialDriveState();
            endDifferentialDrive.LeftWheelPositionInMeters = 10;
            endDifferentialDrive.RightWheelPositionInMeters = 100;
            endDifferentialDrive.TimestampInMilliseconds = 20;
            endDifferentialDrive.TimestampInTicks = 30;
            endDifferentialDrive.DeviceId = 100;          

            long startTime = 100;
            long endTime = 200;
            long[] desiredTimes = { 100, 110, 120, 200 };

            for (int i = 0; i < desiredTimes.Length; ++i)
            {
                double interpolationFactor = 1.0d * (desiredTimes[i] - startTime) / (endTime - startTime);
                DifferentialDriveState expected = new DifferentialDriveState();
                expected.LeftWheelPositionInMeters = startDifferentialDrive.LeftWheelPositionInMeters + (endDifferentialDrive.LeftWheelPositionInMeters - startDifferentialDrive.LeftWheelPositionInMeters) * interpolationFactor;
                expected.RightWheelPositionInMeters = startDifferentialDrive.RightWheelPositionInMeters + (endDifferentialDrive.RightWheelPositionInMeters - startDifferentialDrive.RightWheelPositionInMeters) * interpolationFactor;
                expected.TimestampInMilliseconds = (long)(startDifferentialDrive.TimestampInMilliseconds + (endDifferentialDrive.TimestampInMilliseconds - startDifferentialDrive.TimestampInMilliseconds) * interpolationFactor);
                expected.TimestampInTicks = (long)(startDifferentialDrive.TimestampInTicks + (endDifferentialDrive.TimestampInTicks - startDifferentialDrive.TimestampInTicks) * interpolationFactor);
                DifferentialDriveState interpolated = DifferentialDriveState.Interpolate(startTime, endTime, desiredTimes[i], startDifferentialDrive, endDifferentialDrive);
                Assert.AreEqual(expected.LeftWheelPositionInMeters, interpolated.LeftWheelPositionInMeters, MathConstants.ErrorEpsilon);
                Assert.AreEqual(expected.RightWheelPositionInMeters, interpolated.RightWheelPositionInMeters, MathConstants.ErrorEpsilon);
                Assert.AreEqual(expected.TimestampInMilliseconds, interpolated.TimestampInMilliseconds);
                Assert.AreEqual(expected.TimestampInTicks, interpolated.TimestampInTicks);

                // Ensure that the device id parameter propagates unchanged. This is more of a sanity check and hence only one parameter is tested.
                Assert.AreEqual(startDifferentialDrive.DeviceId, interpolated.DeviceId);
            }
        }
    }
}
